package com.leyou.service;


import com.leyou.cart.pojo.Cart;
import com.leyou.common.auth.pojo.UserHolder;
import com.leyou.common.auth.pojo.UserInfo;
import com.leyou.common.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class CartService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 登录用户添加购物车到redis
     * @param cart
     */
    public void addCart(Cart cart) {
        // 1.获取用户登录的购物车
        BoundHashOperations<String,Object,Object> boundHashOps = getLoginUserCart();

        // 2.判断用户的购物车中是否存在当前skuId
        String skuId = cart.getSkuId().toString();
        if(boundHashOps.hasKey(skuId)){
            // 3.如果存在skuId,则更新购买数量
            // 算法:原商品数量 + 现购买数量

            // 取出原商品数量
            Object cartObj = boundHashOps.get(skuId);
            // 把json字符串转换为Cart对象
            Cart oldCart = JsonUtils.toBean(cartObj.toString(),Cart.class);
            cart.setNum(oldCart.getNum() + cart.getNum());
        }

        // 4.把更新后的商品保存到redis
        boundHashOps.put(skuId,JsonUtils.toString(cart));
    }

    /**
     * 获取当前登录用户的购物车数据
     * @return
     */

    private BoundHashOperations<String, Object, Object> getLoginUserCart() {
        // 1.获取当前登录用户信息
        UserInfo userInfo = UserHolder.getUser();

        // 2.根据userId获取购物车数据（Map结构）
        String userId = userInfo.getId().toString();
        BoundHashOperations<String,Object,Object> boundHashOps = redisTemplate.boundHashOps(userId);
        return boundHashOps;

    }

    public List<Cart> loadCarts() {
        // 1.获取用户登录的购物车
        BoundHashOperations<String,Object,Object> boundHashOps = getLoginUserCart();

        // 2.取出hash结构的value值
        List<Cart> carts = boundHashOps.values().stream()
                .map(cartStr -> JsonUtils.toBean(cartStr.toString(),Cart.class))
                .collect(Collectors.toList());

        return carts;
    }

    public void updateNum(Long skuId, Integer num) {
        // 1.获取登录用户的购物车
        BoundHashOperations<String,Object,Object> boundHashOps = getLoginUserCart();

        // 2.从用户的购物车中取出指定商品
        String skuIdStr = skuId.toString();

        Object cartObj = boundHashOps.get(skuIdStr);
        // 转换为Cart对象
        Cart cart = JsonUtils.toBean(cartObj.toString(),Cart.class);

        //修改数量
        cart.setNum(num);

        //修改用户的购物车数据
        boundHashOps.put(skuIdStr,JsonUtils.toString(cart));
    }


    public void deleteCart(Long skuId) {
        //1.获取登录用户的购物车
        BoundHashOperations<String, Object, Object> boundHashOps = getLoginUserCart();

        //2.从购物车中删除指定商品
        boundHashOps.delete(skuId.toString());
    }

    public void mergeCarts(List<Cart> carts) {
        carts.forEach(cart -> {
            addCart(cart);
        });
    }
}
